The same as wrap, for whole numbers: steps value off one end of [min, max] and reappears at the other, and the answer is a whole number. Use this one for a menu row or a counter that loops - `row = wrap_int(row + 1, 0, rows - 1);` - so the variable you keep it in stays a whole number. NOT a modulo wrap - see the "On the GBA" note before reaching for this the way you would % elsewhere.
wrap_int(value, min, max) -> int| Name | Type |
|---|---|
value | int |
min | int |
max | int |
int
The same as wrap, for whole numbers: steps value off one end of [min, max] and reappears at the other - and the answer is a whole number. Built for the "next/previous item in a small loop" shape: a menu row that goes from the last row back to the first, a picture number that cycles, any counter that should never leave its own range.
Reach for this one whenever the thing you are looping is counted rather than measured - which row of a menu is picked is the classic. Use wrap for things that are measured.
This is not a modulo wrap. A value far past max lands on min in one step, not on min + (value - min) % (max - min). See wrap for the full account; wrap_int follows exactly the same rule.
var row_count = 4;
var next_row = wrap_int(3 + 1, 0, row_count - 1);Moves a menu cursor down from row 3, the last of four rows, and loops it back to row 0 instead of counting past the end. next_row is a whole number, so it can be handed to anything that wants one. A real menu keeps the picked row as a value of its own, updated the same way (row = wrap_int(row + 1, 0, row_count - 1);), typically inside if (btn_pressed(BTN_DOWN)).
wrap and wrap_int do the same arithmetic. They differ in the type of the answer, and that difference is worth a function of its own because of what goes wrong without it.
wrap hands back a real. So this line:
row = wrap(row + 1, 0, 3); <- the trap: this is NOT what you want hereturns row into a real - the value is still 0, 1, 2 or 3, but the kind of number has changed. Nothing complains there. The complaint arrives later, somewhere else, wherever row is next handed to something that wants a whole number, such as the frame of draw_sprite, which says which picture of a sprite to show:
Argument 4 to `draw_sprite` should be an int, but this is a real.That error points at a line that looks perfectly fine, and it may be in a different file from the assignment that caused it. Written with wrap_int, it cannot happen.
wrap_int takes whole numbers only. A number with a point in it is a real - even 4.0 - and is refused when the project is checked:
Argument 3 to `wrap_int` should be an int, but this is a real.That is what you get from wrap_int(row, 0, 3.5). If you have a real and want a whole number out of it, round it first with int_floor, int_ceil or int_round. If the numbers really are measured, use wrap instead.
min and max can be given in either orderA reversed pair (min bigger than max) is swapped before anything else happens, so wrap_int always loops between whichever two numbers were actually given. The same promise wrap and clamp_int make.
A couple of comparisons, and a value already inside the range - by far the common case - comes back unchanged. Safe to call every frame.
wrap and wrap_int differ in TYPE, not in arithmetic. wrap hands back a real, so `row = wrap(row + 1, 0, 3)` turns a whole number into a real one, and the next place that wants a whole number refuses it - usually somewhere else entirely, which makes it a puzzle (a menu row is exactly the kind of number that gets handed on to a draw call or a list). wrap_int hands back a whole number, so it cannot happen. The rule is wrap's own: value below min returns max, and value above max returns min, in ONE step however far past the end value actually is - wrap_int(200, 0, 9) is 0, not the 2 a modulo wrap would give - and min/max may arrive either order (a reversed pair is swapped first). All three numbers have to be whole: wrap_int(row, 0, 3.5) is refused when the project is checked, and so is a variable that holds a real, even one that happens to hold 4.0 - round it first with int_floor, int_ceil or int_round. A couple of comparisons, cheap enough for every frame.